package service;

import JDBC.DBUtils;
import com.google.gson.JsonObject;
import method.AccountInformation;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet(name = "GetDeposit")
public class GetDeposit extends HttpServlet {
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setHeader("Access-Control-Allow-Origin", "*");
        /* 允许跨域的请求方法GET, POST, HEAD 等 */
        resp.setHeader("Access-Control-Allow-Methods", "*");
        /* 重新预检验跨域的缓存时间 (s) */
        resp.setHeader("Access-Control-Max-Age", "3600");
        /* 允许跨域的请求头 */
        resp.setHeader("Access-Control-Allow-Headers", "*");
        HttpSession session = req.getSession();

        String PhoneNumber = (String) session.getAttribute("PhoneNumber");
        AccountInformation account = DBUtils.QueryAccountInformation(PhoneNumber);
        double deposit = 0;

        if (account != null) {
            deposit = account.getDeposit();
        }
        JsonObject output = new JsonObject();
        output.addProperty("deposit", deposit);
        PrintWriter out = resp.getWriter();

        out.println(output);
        out.flush();
        out.close();

    }
}
